// 豆瓣评分信息
class DoubanRating {
  final int max;
  final double average;
  final String stars;
  final int min;

  DoubanRating({
    required this.max,
    required this.average,
    required this.stars,
    required this.min,
  });

  factory DoubanRating.fromJson(Map<String, dynamic> json) {
    return DoubanRating(
      max: json['max'] ?? 10,
      average: (json['average'] ?? 0).toDouble(),
      stars: json['stars'] ?? '00',
      min: json['min'] ?? 0,
    );
  }
}

// 豆瓣演员/导演信息
class DoubanPerson {
  final String id;
  final String name;
  final String alt;
  final Map<String, String> avatars;

  DoubanPerson({
    required this.id,
    required this.name,
    required this.alt,
    required this.avatars,
  });

  factory DoubanPerson.fromJson(Map<String, dynamic> json) {
    return DoubanPerson(
      id: json['id']?.toString() ?? '',
      name: json['name']?.toString() ?? '',
      alt: json['alt']?.toString() ?? '',
      avatars: Map<String, String>.from(json['avatars'] ?? {}),
    );
  }
}

// 豆瓣图片信息
class DoubanImages {
  final String small;
  final String medium;
  final String large;

  DoubanImages({
    required this.small,
    required this.medium,
    required this.large,
  });

  factory DoubanImages.fromJson(Map<String, dynamic> json) {
    return DoubanImages(
      small: json['small']?.toString() ?? '',
      medium: json['medium']?.toString() ?? '',
      large: json['large']?.toString() ?? '',
    );
  }
}

class VodItem {
  final int id;
  final String name;
  final String? typeName;
  final String? pic;
  final String? remarks;
  final String? actor;
  final String? director;
  final String? area;
  final String? year;

  VodItem({
    required this.id,
    required this.name,
    this.typeName,
    this.pic,
    this.remarks,
    this.actor,
    this.director,
    this.area,
    this.year,
  });

  factory VodItem.fromJson(Map<String, dynamic> json) {
    return VodItem(
      id: int.tryParse('${json['vod_id'] ?? json['id'] ?? 0}') ?? 0,
      name: '${json['vod_name'] ?? json['name'] ?? ''}',
      typeName: json['type_name']?.toString(),
      pic: json['vod_pic']?.toString(),
      remarks: json['vod_remarks']?.toString(),
      actor: json['vod_actor']?.toString(),
      director: json['vod_director']?.toString(),
      area: json['vod_area']?.toString(),
      year: json['vod_year']?.toString(),
    );
  }
}

class PlayEpisode {
  final String title;
  final String url;
  PlayEpisode(this.title, this.url);
}

class PlaySource {
  final String name;
  final List<PlayEpisode> episodes;
  final String? siteName; // 所属视频站点名称（可选）
  PlaySource(this.name, this.episodes, [this.siteName]);
}

class VodDetail extends VodItem {
  final String? content;
  final List<PlaySource> playSources;
  final String? siteName; // 来源站点名（当来自资源站时）
  
  // 豆瓣扩展字段
  final DoubanRating? rating;
  final List<DoubanPerson> casts;
  final List<DoubanPerson> directors;
  final List<String> countries;
  final List<String> genres;
  final List<String> aka;
  final DoubanImages? images;
  final String? summary;
  final int? reviewsCount;
  final int? wishCount;
  final int? collectCount;
  final int? ratingsCount;
  final int? commentsCount;
  final String? originalTitle;
  final String? subtype;
  final String? doubanUrl;

  VodDetail({
    required super.id,
    required super.name,
    super.typeName,
    super.pic,
    super.remarks,
    super.actor,
    super.director,
    super.area,
    super.year,
    this.content,
    required this.playSources,
    this.siteName,
    this.rating,
    this.casts = const [],
    this.directors = const [],
    this.countries = const [],
    this.genres = const [],
    this.aka = const [],
    this.images,
    this.summary,
    this.reviewsCount,
    this.wishCount,
    this.collectCount,
    this.ratingsCount,
    this.commentsCount,
    this.originalTitle,
    this.subtype,
    this.doubanUrl,
  });

  factory VodDetail.fromJson(Map<String, dynamic> json) {
    return VodDetail(
      id: int.tryParse('${json['vod_id'] ?? json['id'] ?? 0}') ?? 0,
      name: '${json['vod_name'] ?? json['name'] ?? ''}',
      typeName: json['type_name']?.toString(),
      pic: json['vod_pic']?.toString(),
      remarks: json['vod_remarks']?.toString(),
      actor: json['vod_actor']?.toString(),
      director: json['vod_director']?.toString(),
      area: json['vod_area']?.toString(),
      year: json['vod_year']?.toString(),
      content: json['vod_content']?.toString(),
      playSources: const [],
      siteName: null,
    );
  }

  // 从豆瓣API数据创建VodDetail
  factory VodDetail.fromDoubanJson(Map<String, dynamic> json) {
    return VodDetail(
      id: int.tryParse('${json['id'] ?? 0}') ?? 0,
      name: json['title']?.toString() ?? '',
      typeName: (json['genres'] as List?)?.join('、') ?? '',
      pic: json['images']?['large']?.toString(),
      remarks: json['year']?.toString(),
      actor: (json['casts'] as List?)?.map((cast) => cast['name']).join('、') ?? '',
      director: (json['directors'] as List?)?.map((dir) => dir['name']).join('、') ?? '',
      area: (json['countries'] as List?)?.join('、') ?? '',
      year: json['year']?.toString(),
      content: json['summary']?.toString(),
      playSources: const [],
      siteName: null,
      // 豆瓣扩展字段
      rating: json['rating'] != null ? DoubanRating.fromJson(json['rating']) : null,
      casts: (json['casts'] as List?)?.map((cast) => DoubanPerson.fromJson(cast)).toList() ?? [],
      directors: (json['directors'] as List?)?.map((dir) => DoubanPerson.fromJson(dir)).toList() ?? [],
      countries: List<String>.from(json['countries'] ?? []),
      genres: List<String>.from(json['genres'] ?? []),
      aka: List<String>.from(json['aka'] ?? []),
      images: json['images'] != null ? DoubanImages.fromJson(json['images']) : null,
      summary: json['summary']?.toString(),
      reviewsCount: json['reviews_count'],
      wishCount: json['wish_count'],
      collectCount: json['collect_count'],
      ratingsCount: json['ratings_count'],
      commentsCount: json['comments_count'],
      originalTitle: json['original_title']?.toString(),
      subtype: json['subtype']?.toString(),
      doubanUrl: json['alt']?.toString(),
    );
  }
}